clear 
est clear

**********************************************************************
* Balance 
**********************************************************************

run "${code}\Programs\texsave_custom.do"

*** Prep data  *********************

use "$data\SMU_CleanData_Panel_Public", replace
sort cr_id location_branch location_club round

* again keep just baseline data 
keep if round==0

* label treatment nicely 	
label define randomization 0 "Control" 1 "IPT-G" 2 "IPT-G+", modify
label values randomization randomization
tab randomization


*** Create missing baseline vars ******************* 

*** ever pregnant 
fre s_eversex
fre f_everpreg
tab f_everpreg s_eversex, m 
* ever pregnant is zero if never had sex 
gen everpregnant = 0 		if f_everpreg==0 | s_eversex==0
replace everpregnant = 1 	if f_everpreg==1  
* is missing in 51 cases, due to either sex of preg Q's missing 
tab everpregnant
mdesc everpregnant


*** risky_sex 
fre s_eversex
fre s_lastcondom
tab s_eversex s_lastcondom, m
* used condom last time is zero if never had sex 
gen riskysex = 0 		if s_lastcondom==0 | s_eversex==0
replace riskysex = 1 	if s_lastcondom==1  
* is missing in 51 cases, due to either sex of preg Q's missing 
tab riskysex
mdesc riskysex


*** in school 
*	can't really figure it out... just keep as it
tab ed_inschool 
gen missing_inschool = missing(ed_inschool)
bys missing_inschool: su CR_age ,
* I think this is it, after some digging, but check cleaning do files... 
replace ed_inschool = 0 if missing(ed_inschool) & !missing(ed_highestlevel) &   missing(ed_levelattended_insess) &  missing(ed_levelattended_notinsess)
replace ed_inschool = 1 if missing(ed_inschool) &  missing(ed_highestlevel) & (!missing(ed_levelattended_insess) | !missing(ed_levelattended_notinsess))
replace ed_inschool = 1 if missing(ed_inschool) &  missing(ed_highestlevel) & (!missing(ed_levelattended_insess) | !missing(ed_levelattended_notinsess))

*** ever married
*	count `as if' married as married
*	means divorced/separated/widowed are not married... 
fre ms_currentlymarried
gen evermarried = inlist(ms_currentlymarried,1,2,3,4,5,6) if !missing(ms_currentlymarried) & ms_currentlymarried>=0
tab ms_currentlymarried evermarried, m



*** vars that still have issues 
*	ignore for now...
su f_hopedyrs ms_yrshoped 


*** Create balance table ******************* 

# delimit ;
local balvars = "phq8_score GHQ12_score rosb_score rscore 
	CR_age PPI_score evermarried everpregnant 
	comp_score ed_inschool riskysex" ;
# delimit cr 

*** label nicely 
label var phq8_score "PHQ-8 Score (0-24)"
label var GHQ12_score "GHQ-12 Score (0-12)"
label var rosb_score "Rosenberg Self Esteem Score (0-40)"
label var rscore "Child&Youth Resilience Score (0-34)"
label var CR_age "Age in years"
label var PPI_score "Poverty Probability Index (0-100)"
label var ed_inschool "=1 if Enrolled in School"
label var comp_score "Core Competencies Score (0-4)"
label var evermarried "=1 if Ever Married"
label var everpregnant "=1 if Ever Pregnant"
label var riskysex "=1 if Risky Sex" 

*** check the balance variables JIC
mdesc `balvars' 
su `balvars'

*** Table 
iebaltab `balvars' , grpvar(randomization) browse ///
	nonote rowvarlabels nostars ftest /// feqtest /// control(0)
	stats(desc(sd) pair(p) f(p) feq(p)) ///
	fixedeffect(block)  vce(cluster location_club)
* drop N columns 
drop v2 v4 v6 v8 v10 v12
drop if _n>26
* remove stars from the table, 
ds 
foreach v in `r(varlist)' {
	replace `v' = subinstr(`v',"*","",.)
}
* check the table, will export to latex with the nice command...
browse 

#delimit ;
texsave_custom using "${tables}/balance.tex" , replace nonames  frag
	align("p{0.35\textwidth}*{6}{>{\centering\arraybackslash}m{0.086\textwidth}}")
	;
#delimit cr
* export excel using "${tables}/T-Balance.xls", sheetmodify sheet("Balance") keepcellfmt 

*/

